package com.csi.service.impl;

import com.csi.dao.RoleDao;
import com.csi.dao.impl.RoleDaoImpl;
import com.csi.domain.Role;
import com.csi.service.RoleService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class RoleServiceImpl implements RoleService {
    @Override
    public Map<String, Role> findAll() {
        RoleDao roleDao = new RoleDaoImpl();

        Map<String, Role> roleList = null;
        try {
            roleList = roleDao.findAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return roleList;
    }

    @Override
    public Map<String, Role> findById(int roleId) {
        RoleDao roleDao = new RoleDaoImpl();

        Map<String, Role> roleMap = null;
        try {
            roleMap = roleDao.findById(roleId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return roleMap;
    }

    @Override
    public void updateDesc(int roleId, String roleDesc) {
        RoleDao roleDao = new RoleDaoImpl();

        try {
            roleDao.updateDesc(roleId, roleDesc);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updatePermission(int roleId, List<Integer> permissions) {
        RoleDao roleDao = new RoleDaoImpl();

        try {
            roleDao.updatePermission(roleId, permissions);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void add(String roleName, String roleDesc) {

        RoleDao roleDao = new RoleDaoImpl();

        try {
            roleDao.add(roleName,roleDesc);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}